﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using InterpriseSuiteEcommerceCommon;
using InterpriseSuiteEcommerceCommon.DTO;
using InterpriseSuiteEcommerceCommon.InterpriseIntegration;
using InterpriseSuiteEcommerceGateways;

namespace InterpriseSuiteEcommerce
{
    public partial class contactus : SkinBase
    {
        protected override void OnInit(EventArgs e)
        {
            btnSendMessage.Click += (sender, ex) => SendMessage();
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitPageContent();
        }


        protected void SendMessage()
        {

            try
            {

                if (!IsSecurityCodeGood(txtCaptcha.Text))
                {
                    errorSummary.DisplayErrorMessage(AppLogic.GetString("createaccount.aspx.126", ThisCustomer.SkinID, ThisCustomer.LocaleSetting, true));
                    return;
                }

                if (AppLogic.OnLiveServer() && (Request.UrlReferrer == null || Request.UrlReferrer.Authority != Request.Url.Authority))
                {
                    Response.Redirect("default.aspx", true);
                    return;
                }

                var content = new StringBuilder();

                content.Append(txtMessageDetails.Text);

                content.Append("<br/>");
                content.Append("<br/>");

                content.AppendFormat("Contact Name: {0}", txtContactName.Text);
                content.Append("<br/>");
                content.AppendFormat("Phone: ({0}) {1}", txtAreaCode.Text, txtPrimaryPhone.Text);
                content.Append("<br/>");
                content.AppendFormat("Email Address: {0}", txtEmail.Text);

                string formContents = CommonLogic.FormCanBeDangerousContent(content.ToString());

                string subject = CommonLogic.FormCanBeDangerousContent("Subject", true);

                if (subject.Length == 0)
                {
                    subject = AppLogic.GetString("sendform.aspx.2", SkinID, ThisCustomer.LocaleSetting, true);
                }

                string sendTo = CommonLogic.FormCanBeDangerousContent("SendTo");
                if (sendTo.Length == 0)
                {
                    sendTo = AppLogic.AppConfig("GotOrderEMailTo");
                }
                else
                {
                    sendTo += "," + AppLogic.AppConfig("GotOrderEMailTo");
                }

                foreach (string s in sendTo.Split(','))
                {
                    AppLogic.SendMail(subject, content.ToString(), true, AppLogic.AppConfig("GotOrderEMailFrom"), AppLogic.AppConfig("GotOrderEMailFromName"), s, s, "", AppLogic.AppConfig("MailMe_Server"));
                }

                pnlPageContentWrapper.Visible = false;
                pnlThankYouPage.Visible = true;
               
            }
            catch (Exception ex)
            {
                errorSummary.DisplayErrorMessage(ex.Message);
            }
        }


        protected bool IsSecurityCodeGood(string code)
        {
        
            if (!AppLogic.AppConfigBool("SecurityCodeRequiredOnContactUs")) return true;

            if (Session["SecurityCode"] != null)
            {

                string sCode = Session["SecurityCode"].ToString();
                string fCode = code;

                if (AppLogic.AppConfigBool("Captcha.CaseSensitive"))
                {
                    if (fCode.Equals(sCode)) return true;
                }
                else
                {
                    if (fCode.Equals(sCode, StringComparison.InvariantCultureIgnoreCase)) return true;
                }

                return false;
            }

            return true;

        }

        protected void InitPageContent()
        {
            SectionTitle = AppLogic.GetString("contactus.aspx.1", ThisCustomer.SkinID, ThisCustomer.LocaleSetting, true);
            btnSendMessage.Text = AppLogic.GetString("contactus.aspx.11", ThisCustomer.SkinID, ThisCustomer.LocaleSetting, true);

            if (!AppLogic.AppConfigBool("SecurityCodeRequiredOnContactUs")) pnlSecurityCode.Visible = false;

            if (ThisCustomer.IsInEditingMode())
            {
                AppLogic.EnableButtonCaptionEditing(btnSendMessage, "contactus.aspx.11");
            }

            ContactUsFormHelpFulTipsTopic.SetContext = this;
            ContactUsFormThankYouPageTopic.SetContext = this;
        }

    }
}